package com.itcast.activiti.handler;

import cn.itcast.ic.common.bean.CommonErrorInfo;
import cn.itcast.ic.common.bean.Response;
import cn.itcast.ic.common.exception.ICException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.AccessDeniedException;

/**
 * 全局统一异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 全局异常
     *
     * @param ex
     * @throws IOException
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public void exception(Exception ex, HttpServletResponse response, HttpServletRequest request) throws IOException {
        sendWarningMessage(ex, 30);
        response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
    }

    /**
     * 慎用
     * 拦截所有运行时的全局异常
     * 未知runtime异常
     * throw new RuntimeException("未知异常使用");
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public @ResponseBody
    Object runtimeException(RuntimeException ex) {
        if (ex instanceof ICException) {
            ICException ice = (ICException) ex;
            if (ice.getErrorInfo() != null) {
                return Response.error(ice.getErrorInfo());
            }
        }
        return Response.error(CommonErrorInfo.UNKOWN_ERROR.getErrorCode(), ex.getMessage());
    }

    /**
     * 参数缺失错误
     * Assert.notNull(olnEpPaySuborder, "参数缺失异常");
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public @ResponseBody
    Object illegalArgumentException(IllegalArgumentException ex) {
        sendWarningMessage(ex, 9);
        return Response.error(CommonErrorInfo.PARAMETER_ERROR.getErrorCode(), ex.getMessage());
    }

    /**
     * 请求方式不支持错误
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public @ResponseBody
    Object httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
        return Response.error(CommonErrorInfo.unkownError(ex.getMessage()));
    }


    /**
     * 请求方式不支持错误
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    public @ResponseBody
    Object accessDeniedException(AccessDeniedException ex) {
        return Response.error(CommonErrorInfo.UNAUTHORIZED);
    }

    private void sendWarningMessage(Exception ex, int depth) {
        logger.info("throw an exception : " + ex.getMessage());
    }


}